package com.jeesite.modules.sd.service;

import com.google.zxing.WriterException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jeesite.common.config.Global;
import com.jeesite.modules.sd.common.util.PdfFontUtils;
import com.jeesite.modules.sd.common.util.PrintUtil;
import com.jeesite.modules.sd.common.util.QRBarCodeUtil;
import com.jeesite.modules.sd.entity.Checklist;
import org.springframework.stereotype.Service;

import javax.print.DocFlavor;
import javax.print.PrintException;
import javax.print.attribute.standard.MediaSizeName;
import java.io.*;

/**
 * 打印二维码
 *
 * @author boya
 * @version 2020-07-15
 */
@Service
public class PrintService {

    /**
     * 生成二维码并打印（二维码不保存到本地）
     *
     * @param codeContent           二维码内容
     * @param fontSize              文字号
     * @param byteArrayOutputStream 输出流
     * @param fullNum              文号
     * @param letterBox             信箱号
     * @param unit                  单位
     * @throws IOException
     * @throws WriterException
     * @throws PrintException
     */
    public void printCode(String codeContent, String fullNum,
                          String letterBox, String unit,
                          int fontSize, ByteArrayOutputStream byteArrayOutputStream) throws IOException, WriterException, PrintException {
        // 生成二维码
        QRBarCodeUtil.createQRCodeWithWordsToStream(codeContent, 550, 590
                , fullNum, letterBox, unit, fontSize, byteArrayOutputStream);
        // 转inputStream
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        // 打印
        PrintUtil.print(inputStream, MediaSizeName.ISO_A8, DocFlavor.INPUT_STREAM.PNG, Global.getConfig("doc.code.printer"));
        byteArrayOutputStream.close();

//        String filePath = "D:\\" + fullNum+"-" + unit + ".png";
//        QRBarCodeUtil.createQRCodeWithWordsToFile(codeContent, 550, 570, filePath
//                , fullNum, letterBox, unit, fontSize);
    }

    /**
     * 打印清单PDF
     * @param checklist 清单内容
     * @param outputStream 输出流
     * @throws FileNotFoundException
     * @throws DocumentException
     * @throws PrintException
     */
    public void printCheckList(Checklist checklist,ByteArrayOutputStream outputStream) throws IOException, DocumentException, PrintException {
        //1 创建Document
        Document document = new Document();
        //2 获取PdfWriter
        PdfWriter.getInstance(document, outputStream);
        //3 打开
        document.open();
        //4 添加内容
        document.add(PdfFontUtils.getFont(1, checklist.getUnitName()+"还文清单"));
        //打印还文列表
        PdfPTable file = new PdfPTable(new float[]{240,80,80});
        createFileHead(new Phrase(PdfFontUtils.getFont(4, "文件标题")),file);
        createFileHead(new Phrase(PdfFontUtils.getFont(4, "来文文号")),file);
        createFileHead(new Phrase(PdfFontUtils.getFont(4, "来文单位")),file);

        checklist.getFileInfos().forEach(info->{
            createFileCell(new Phrase(PdfFontUtils.getFont(5, info.getTitle())),file);
            createFileCell(new Phrase(PdfFontUtils.getFont(5, info.getFullNum())),file);
            createFileCell(new Phrase(PdfFontUtils.getFont(5, info.getUnitName())),file);
        });

        createFileFoot(new Phrase(PdfFontUtils.getFont(5, "合计："+checklist.getFileInfos().size())),file);

        document.add(file);
        //添加两个空行
        document.add(PdfFontUtils.getFont(3,""));
        document.add(PdfFontUtils.getFont(3,""));
        //用于装文件名称清单的table
        PdfPTable table = new PdfPTable(2);
        table.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
        createCell(new Phrase(PdfFontUtils.getFont(5, "还文人："+checklist.getReturnUserName())),table);
        createCell(new Phrase(PdfFontUtils.getFont(5, "保密员：_______")),table);
        createCell(new Phrase(PdfFontUtils.getFont(5, "还文人警号："+checklist.getReturnUserPoliceNo())),table);
        createCell(new Phrase(PdfFontUtils.getFont(5, "还文时间："+checklist.getReturnDate())),table);

        document.add(table);
        //5 关闭
        document.close();
        // 转inputStream
        InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

        PrintUtil.print(inputStream,MediaSizeName.ISO_A4, DocFlavor.INPUT_STREAM.AUTOSENSE, Global.getConfig("doc.pdf.printer"));
        outputStream.close();
    }

    /**
     * 将文本设置到table中(此方法只适用于还文清单的格式)
     * @param phrase cell内容
     *
     */
    private void createCell(Phrase phrase,PdfPTable table){
        PdfPCell cell = new PdfPCell(phrase);
        cell.setColspan(1);
        cell.setBorder(0);
        cell.setPaddingTop(10);
        table.addCell(cell);
    }



    private void createFileHead(Phrase phrase,PdfPTable table){
        PdfPCell cell = new PdfPCell(phrase);
        cell.setColspan(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }
    /**
     * 将文本设置到table中(此方法只适用于还文清单的格式)
     * @param phrase cell内容
     *
     */
    private void createFileCell(Phrase phrase,PdfPTable table){
        PdfPCell cell = new PdfPCell(phrase);
        cell.setColspan(1);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }

    private void createFileFoot(Phrase phrase,PdfPTable table){
        PdfPCell cell = new PdfPCell(phrase);
        cell.setColspan(3);
        cell.setVerticalAlignment(Element.ALIGN_CENTER);
        table.addCell(cell);
    }
}
